package com.sqfw.project.wp.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.sqfw.common.utils.DateUtils;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.web.domain.TreeSelect;
import com.sqfw.project.wp.domain.SysCode;
import com.sqfw.project.wp.domain.WpTreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wp.mapper.SysStruBakMapper;
import com.sqfw.project.wp.domain.SysStruBak;
import com.sqfw.project.wp.service.ISysStruBakService;

/**
 * 部门字典表Service业务层处理
 * 
 * @author lx
 * @date 2023-06-09
 */
@Service
public class SysStruBakServiceImpl implements ISysStruBakService 
{
    @Autowired
    private SysStruBakMapper sysStruBakMapper;

    /**
     * 查询部门字典表
     * 
     * @param autoId 部门字典表主键
     * @return 部门字典表
     */
    @Override
    public SysStruBak selectSysStruBakByAutoId(String autoId)
    {
        return sysStruBakMapper.selectSysStruBakByAutoId(autoId);
    }

    /**
     * 查询部门字典表列表
     * 
     * @param sysStruBak 部门字典表
     * @return 部门字典表
     */
    @Override
    public List<SysStruBak> selectSysStruBakList(SysStruBak sysStruBak)
    {
        return sysStruBakMapper.selectSysStruBakList(sysStruBak);
    }

    /**
     * 新增部门字典表
     * 
     * @param sysStruBak 部门字典表
     * @return 结果
     */
    @Override
    public int insertSysStruBak(SysStruBak sysStruBak)
    {
        sysStruBak.setCreateTime(DateUtils.getNowDate());
        return sysStruBakMapper.insertSysStruBak(sysStruBak);
    }

    /**
     * 修改部门字典表
     * 
     * @param sysStruBak 部门字典表
     * @return 结果
     */
    @Override
    public int updateSysStruBak(SysStruBak sysStruBak)
    {
        sysStruBak.setUpdateTime(DateUtils.getNowDate());
        return sysStruBakMapper.updateSysStruBak(sysStruBak);
    }

    /**
     * 批量删除部门字典表
     * 
     * @param autoIds 需要删除的部门字典表主键
     * @return 结果
     */
    @Override
    public int deleteSysStruBakByAutoIds(String[] autoIds)
    {
        return sysStruBakMapper.deleteSysStruBakByAutoIds(autoIds);
    }

    /**
     * 删除部门字典表信息
     * 
     * @param autoId 部门字典表主键
     * @return 结果
     */
    @Override
    public int deleteSysStruBakByAutoId(String autoId)
    {
        return sysStruBakMapper.deleteSysStruBakByAutoId(autoId);
    }

    @Override
    public List<WpTreeSelect> selectSysStruBakTreeList(SysStruBak sysStruBak) {
        List<SysStruBak> sysStruBaks = sysStruBakMapper.selectSysStruBakList(sysStruBak);
        return buildSysCodeTreeSelect(sysStruBaks);
    }

    public List<WpTreeSelect> buildSysCodeTreeSelect(List<SysStruBak> sysStruBaks)
    {
        List<SysStruBak> deptTrees = buildSysCodeTree(sysStruBaks);
        return deptTrees.stream().map(WpTreeSelect::new).collect(Collectors.toList());
    }

    public List<SysStruBak> buildSysCodeTree(List<SysStruBak> sysStruBaks)
    {
        List<SysStruBak> returnList = new ArrayList<SysStruBak>();
        List<Long> tempList = sysStruBaks.stream().map(SysStruBak::getAutoId).collect(Collectors.toList());
        for (SysStruBak sysStruBak : sysStruBaks)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(sysStruBak.getPid()))
            {
                recursionFn(sysStruBaks, sysStruBak);
                returnList.add(sysStruBak);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = sysStruBaks;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysStruBak> list, SysStruBak sysStruBak)
    {
        // 得到子节点列表
        List<SysStruBak> childList = getChildList(list, sysStruBak);
        sysStruBak.setChildren(childList);
        for (SysStruBak tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysStruBak> list, SysStruBak sysStruBak)
    {
        return getChildList(list, sysStruBak).size() > 0 ? true : false;
    }

    /**
     * 得到子节点列表
     */
    private List<SysStruBak> getChildList(List<SysStruBak> list, SysStruBak sysStruBak)
    {
        List<SysStruBak> tlist = new ArrayList<SysStruBak>();
        Iterator<SysStruBak> it = list.iterator();
        while (it.hasNext())
        {
            SysStruBak n = (SysStruBak) it.next();
            if (StringUtils.isNotNull(n.getPid()) && n.getPid().longValue() == sysStruBak.getAutoId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
}
